home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Eagles Nest BBS 5
/
Eagles_Nest_Mac_Collection_Disc_5.TOAST
/
Math & Engineering
/
MacBrain110
/
LoadAndSave.p
< prev
next >
Wrap
Text File
|
1987-10-21
|
10KB
|
348 lines
unit LoadAndSave;
INTERFACE
USES
Memtypes, Quickdraw, OSIntf, ToolIntf, PackIntf, PasLibIntf, SANE;
{To use this unit, you must declare global variables to store the network}
{in. Use Appendix A in your MacBrain™ manual to check which data types }
{are necessary. We didn't include these declarations; this allows you }
{more freedom in designing your application. You also need the following}
{functions: }
{ GetWeight(y,x:integer):real returns weight from unit y to unit x}
{ GetMLinkUnit(y,x:integer):integer returns unit that modulates weight from unit y to unit x}
{ GetMLinkValue(y,x:integer):real returns value of m-link on weight from unit y to unit x }
{and the following procedures: }
{ SetWeight(y,x:integer; weight:real) sets weight from unit y to unit x to weight}
{ SetMLink(y,x,my:integer; mweight:real) sets m-link unit on weight from unit y to unit x to be}
{ equal to my, and the m-link value equal to mweight.}
{In addition, you should add a procedure 'note(message:str255)' that will}
{display an alert containing 'message'. This is not crucial, however.}
{These routines will give the window 'TheWindow' a title equivalent to }
{the most-recently loaded or saved file name.}
VAR
theReply : SFReply;
theMessage:integer;
nDocs: integer;
countDocs:integer;
docInfo:AppFile;
PROCEDURE actually_save; {Used by save_file.}
PROCEDURE save_file; {Call this from your file menu.}
PROCEDURE actually_load; {Used by load_file.}
PROCEDURE load_file; {Call this from your file menu.}
PROCEDURE open_from_finder; {Opens a file if application was launched}
{from finder by opening the file.}
IMPLEMENTATION
PROCEDURE actually_save;
var
resultCode:OSErr;
theInfo : FInfo;
x,y:integer;
BEGIN
if file_name<>'Untitled' then
begin
rewrite(file_of_units, file_name);
writeln(file_of_units, '110');
writeln(file_of_units, total_groups);
FOR x := 1 TO total_groups DO
writeln(file_of_units, group_names[x]);
writeln(file_of_units, DefaultActFunction);
writeln(file_of_units, LearnDef);
writeln(file_of_units, DefaultGroup);
writeln(file_of_units, DefaultThreshold);
writeln(file_of_units, DefaultActivation);
writeln(file_of_units, DefaultDecay);
writeln(file_of_units, DefaultInstrument);
writeln(file_of_units, DefaultWeightDisplay);
writeln(file_of_units, DefaultUpdateFrequency);
IF DefaultDisplayNames THEN
writeln(file_of_units, 'T')
ELSE
writeln(file_of_units, 'F');
IF DefaultSymLinks THEN
writeln(file_of_units, 'T')
ELSE
writeln(file_of_units, 'F');
IF DefaultClamp THEN
writeln(file_of_units, 'T')
ELSE
writeln(file_of_units, 'F');
writeln(file_of_units, total_units);
FOR x := 1 TO total_units DO
WITH units[x]^ DO
BEGIN
writeln(file_of_units, name);
writeln(file_of_units, instrument);
writeln(file_of_units, group);
writeln(file_of_units, locX);
writeln(file_of_units, locY);
writeln(file_of_units, threshold);
writeln(file_of_units, decay);
writeln(file_of_units, activation);
writeln(file_of_units, activation2);
writeln(file_of_units, last_activation);
writeln(file_of_units, output);
writeln(file_of_units, other_thing);
writeln(file_of_units, clamped);
writeln(file_of_units, update_frequency);
IF boolean1 THEN
writeln(file_of_units, 'T')
ELSE
writeln(file_of_units, 'F');
IF boolean2 THEN
writeln(file_of_units, 'T')
ELSE
writeln(file_of_units, 'F');
FOR y := 1 TO total_units DO
IF GetWeight(y,x) <> 0 THEN
BEGIN
writeln(file_of_units, y);
writeln(file_of_units, GetWeight(y,x));
writeln(file_of_units, GetMLinkUnit(y,x));
writeln(file_of_units, GetMLinkValue(y,x));
END;
writeln(file_of_units, 0);
writeln(file_of_units, 0.0);
writeln(file_of_units, 0);
writeln(file_of_units, 0.0); { end of weights }
END; { end units loop }
close(file_of_units);
theInfo.fdType := 'NNE1';
theInfo.fdCreator := 'MNJ1';
resultCode := SetFInfo(theReply.fName, theReply.vRefNum, theInfo);
IOCheck(resultCode);
SetWTitle(TheWindow, file_name);
InitCursor;
end;
END;
PROCEDURE save_file;
CONST
wrongTypeID = 1004;
VAR
x, y, zz : integer;
dlgOrigin : Point;
string1 : STRING[70];
MyParamBlock: WDPBPtr;
theDirRefNum:integer;
MyRefNum:integer;
theFile : integer;
ignore : integer;
theInfo : FInfo;
resultCode : OSErr;
BEGIN
SetPt(dlgOrigin, DlgLeft, DlgTop);
theTypeList[0] := 'NNE1';
if file_name='Untitled' then
SFPutFile(dlgOrigin, 'Save model as what ?', 'model name', NIL, theReply)
else
SFPutFile(dlgOrigin, 'Save model as what ?', file_name, NIL, theReply);
if theReply.fname<>'Untitled' then
WITH theReply DO
BEGIN
IF good THEN
BEGIN
SetCursor(Watch^^);
new(MyParamBlock);
with MyParamBlock^ do
begin
ioNamePtr:=NIL;
ioVRefNum:=vRefNum;
ioWDDirID:=0;{?}
end;
resultCode:=PBHSetVol(MyParamBlock,FALSE);
if resultcode<>0 then note('Error in setting directory.');
file_name := fName;
actually_save;
EnableItem(MyFileMenu,fmsave);
END;
END;
END;
PROCEDURE new_file;
VAR
x : integer;
resultCode : OSErr;
disposeError : boolean;
BEGIN
disposeError := false;
x := 1;
WHILE ((x <= total_units) AND (NOT disposeError)) DO
BEGIN
dispose(units[x]);
resultCode := MemError;
IF resultCode <> 0 THEN
stop('Dispose Error in New_File');
IF resultCode <> 0 THEN
disposeError := true;
x := x + 1;
END;
total_units := 0;
SetPort(TheWindow);
view_update;
file_name:='Untitled';
SetWTitle(TheWindow, 'Untitled');
DisableItem(MyFileMenu,fmsave);
END;
PROCEDURE actually_load;
VAR
x, y,my, z : integer;
weight,mweight : real;
ch : char;
string1 : STRING[70];
resultCode : OSErr;
MyParamBlock: WDPBPtr;
theDirRefNum:integer;
MyRefNum:integer;
theFile : integer;
BEGIN
BEGIN
BEGIN
SetCursor(Watch^^);
new(MyParamBlock);
with MyParamBlock^ do
begin
ioNamePtr:=NIL;
ioVRefNum:=theReply.vRefNum;
ioWDDirID:=0;{?}
end;
resultCode:=PBHSetVol(MyParamBlock,FALSE);
if resultcode<>0 then note('Error in setting directory.');
x := total_units;
IF x > 0 THEN
new_file;
file_name := theReply.fName;
reset(file_of_units, file_name);
readln(file_of_units, total_groups);
readln(file_of_units, total_groups);
FOR x := 1 TO total_groups DO
readln(file_of_units, group_names[x]);
readln(file_of_units, DefaultActFunction);
readln(file_of_units, LearnDef);
readln(file_of_units, DefaultGroup);
readln(file_of_units, DefaultThreshold);
readln(file_of_units, DefaultActivation);
readln(file_of_units, DefaultDecay);
readln(file_of_units, DefaultInstrument);
readln(file_of_units, DefaultWeightDisplay);
readln(file_of_units, DefaultUpdateFrequency);
{readln(file_of_units, total_graphs);}
{for z:=1 to total_graphs do}
{ readln(file_of_units, DefaultGraph[z]);}
readln(file_of_units, ch);
IF ch = 'T' THEN
DefaultDisplayNames := True
ELSE
DefaultDisplayNames := False;
readln(file_of_units, ch);
IF ch = 'T' THEN
DefaultSymLinks := True
ELSE
DefaultSymLinks := False;
readln(file_of_units, ch);
IF ch = 'T' THEN
DefaultClamp := True
ELSE
DefaultClamp := False;
readln(file_of_units, total_units);
FOR x := 1 TO total_units DO
new(units[x]);
FOR x := 1 TO total_units DO
BEGIN
WITH units[x]^ DO
BEGIN
readln(file_of_units, name);
readln(file_of_units, instrument);
readln(file_of_units, group);
readln(file_of_units, locX);
readln(file_of_units, locY);
readln(file_of_units, threshold);
readln(file_of_units, decay);
readln(file_of_units, activation);
readln(file_of_units, activation2);
readln(file_of_units, last_activation);
readln(file_of_units, output);
readln(file_of_units, other_thing);
readln(file_of_units, clamped);
readln(file_of_units, update_frequency);
readln(file_of_units, ch);
IF ch = 'T' THEN
boolean1 := True
ELSE
boolean1 := False;
readln(file_of_units, ch);
IF ch = 'T' THEN
boolean2 := True
ELSE
boolean2 := False;
WeightBase:=NIL;
y := 1;
WHILE y <> 0 DO
BEGIN
readln(file_of_units, y);
readln(file_of_units, weight);
readln(file_of_units, my);
readln(file_of_units, mweight);
IF y <> 0 THEN
BEGIN
SetWeight(y,x,weight);
SetMLink(y,x,my,mweight);
END;
END;
END;
END;
END;
close(file_of_units);
SetWTitle(TheWindow, file_name);
EnableItem(MyFileMenu,fmsave);
END;
END;
PROCEDURE load_file;
VAR
dlgOrigin : Point;
BEGIN
SetPt(dlgOrigin, DlgLeft, DlgTop);
theTypeList[0] := 'NNE1';
SFGetFile(dlgOrigin, 'Load which model ?', NIL, 1, theTypeList, NIL, theReply);
IF theReply.good THEN
actually_load;
END;
PROCEDURE open_from_finder;
BEGIN
CountAppFiles(theMessage,nDocs);
if nDocs>0 then
BEGIN
if theMessage=AppPrint then
ExitToShell
else
begin
GetAppFiles(1,docInfo);
if docInfo.fType='NNE1' then
begin
theReply.fName:=docInfo.fName;
theReply.vRefNum:=docInfo.vRefNum;
actually_load;
for countDocs:= 1 to nDocs do ClrAppFiles(CountDocs);
end
else
begin
Stop('File must be converted to 1.10 format.');
ExitToShell;
end;
end;
END;
End; {open_from_finder}
END. {implementation}